package com.lzj.server.service.impl;

import com.lzj.server.config.RabbitMQConfig;
import com.lzj.server.entity.dto.mq.UserActionDTO;
import com.lzj.server.entity.dto.mq.VideoProcessDTO;
import com.lzj.server.service.RabbitMQProducerService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Service;

@Service
@Slf4j
@RequiredArgsConstructor
public class RabbitMQProducerServiceImpl implements RabbitMQProducerService {

    private final RabbitTemplate rabbitTemplate;

    @Override
    public void sendVideoUploadedEvent(VideoProcessDTO event) {
        String routingKey = "video.uploaded";
        try {
            rabbitTemplate.convertAndSend(RabbitMQConfig.EXCHANGE_VIDEO, routingKey, event);
            log.info("视频上传事件发送成功. RoutingKey: {}, Event: {}", routingKey, event);
        } catch (Exception e) {
            log.error("视频上传事件发送失败: {}", event, e);
        }
    }

    @Override
    public void sendUserActionEvent(UserActionDTO event) {
        // user.action.{action}.{userId}
        String routingKey = String.format("user.action.%s.%d", event.action(), event.userId());
        try {
            rabbitTemplate.convertAndSend(RabbitMQConfig.EXCHANGE_USER_ACTION, routingKey, event);
            log.info("用户行为事件发送成功. RoutingKey: {}, Event: {}", routingKey, event);
        } catch (Exception e) {
            log.error("用户行为事件发送失败: {}", event, e);
        }
    }
}